package loader 
{
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.net.URLRequest;
	import flash.events.IOErrorEvent;
	/**
	 * ...
	 * @author FireKnight
	 */
	public class SinglePicLoader extends EventDispatcher 
	{
		private var myLoader:Loader;
		private var callBack:Function;
		private var url:String;
		protected var para:*;
		public function SinglePicLoader(s:String="",b:Function=undefined,_para:*=null,target:IEventDispatcher=null) 
		{
			super(target);
			if(s!="")
			{
				callBack = b;
				url = s;
				para=_para;
				loadFile(s);
			}
			
			
		}
		
		private function removeLoader():void
		{
			if (myLoader)
			{
				myLoader = null;
			}
		}

		private function handleIOERROR(e:IOErrorEvent):void
		{
			trace("handleIOERROR");
			trace(url);
		}
		
		public function init(s:String,b:Function,_para:*=null):void
		{
			callBack = b;
			url = s;
			para=_para;
			loadFile(s);
		}
		
		private function loadFile(s:String):void
		{
			removeLoader();
			myLoader = new Loader();
			myLoader.load(new URLRequest(s));
			myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
			myLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, handleIOERROR);
		}
		
		private function onComplete(e:Event):void
		{
			var bm:Bitmap = e.target.content as Bitmap;
			
			if (bm == null)
			{
				
			}
			else
			{
				bm.smoothing = true;
			}
			if(para!=null)
			{
				this.callBack(bm,para);
			}
			else
			{
				this.callBack(bm);
			}
				
			e.target.loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onComplete);
			e.target.loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, handleIOERROR);
			removeLoader();
		}

	}

}